package com.amox.uitest.kit;

import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrKit {

	private final static StrKit kit = new StrKit();
	
	private StrKit() {}
	
	public static StrKit me() {
		return kit;
	}
	
	public String filterEL(String str) {
		String ok = regEx("\\$\\{[\\w]+\\}", str);
		Matcher m = Pattern.compile("[\\w]+").matcher(ok);
		if(m.find()) {
			return m.group();
		}
		return str;
	}
	
	public String regEx(String regEx, String str) {
		Pattern  pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(str);
		if(matcher.find()) {
			return matcher.group();
		}
		return str;
	}
	
	public String toSingleLine(String str) {
		return str.replaceAll("\n\t\r", " ");
	}
	
	public String error(Object... args) {
		return StrKit.me().format("ERROR : The Node : {0} Is Not Found, Or It''s Value Is Invalid !", args);
	}
	
	public String warn(Object... args) {
		return StrKit.me().format("WARNING : The Node[{0}] Is Not Found, Or It''s Value Is Invalid !", args);
	}
	
	public String upperFirst(String str) {
		char[] cs = str.toCharArray();
		
		if(!('a' <= cs[0] && cs[0] <= 'z')) {
			return str;
		}
		
		cs[0] -= 32;
		return String.valueOf(cs);
	}
	
	public String format(String pattern, Object... arguments) {
		return MessageFormat.format(pattern, arguments);
	}
}
